package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;

    /**
     * 新增套餐
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 分页条件查询
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> pageInfo = setmealService.pageSetmealDto(page, pageSize, name);
        return R.success(pageInfo);
    }

    /**
     * 批量删除套餐
     */
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids) {
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 启用/停用状态
     */
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        //update setmeal set status=? where id in (ids)
        setmealService.update()
                .set("status", status)
                .in("id", ids)
                .update();
        return R.success("操作成功");
    }

    /**
     * 根据id查询套餐详情信息
     */
    @GetMapping("{id}")
    public R<SetmealDto> findById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.findById(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐信息
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 根据套餐分类查询列表
     */
    @GetMapping("list")
    public R<List<Setmeal>> list(Long categoryId, Integer status) {
        //根据分类查询套餐列表
        List<Setmeal> list = setmealService.query()
                .eq(categoryId != null, "category_id", categoryId)
                .eq(status != null, "status", status)
                .orderByDesc("update_time")
                .list();
        return R.success(list);
    }

    /**
     * 根据id查询菜品列表
     */
    @GetMapping("dish/{setmealId}")
    public R<List<DishDto>> listDishDtoBySetmealId(@PathVariable Long setmealId) {
        List<DishDto> list = dishService.listDishDtoBySetmealId(setmealId);
        return R.success(list);
    }

}
